package com.puzzletest;

import com.puzzletest.bean.ImageInfo;
import com.puzzletest.bean.ImageInfoList;
import com.puzzletest.net.LoadData;
import com.puzzletest.net.Util;
import com.puzzletest.view.CustomImageView;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.PointF;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

public class SingleRecoverActivity extends Activity{
	
	private final Activity activity = this;
	private CustomImageView imageView;
	private Handler mHandler;
	
	private ProgressDialog progress;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_singlerecover);
		
		imageView = (CustomImageView) findViewById(R.id.imageView);
		
		mHandler = new Handler(){
			public void handleMessage(android.os.Message msg) {
				switch (msg.what) {
				case 1:
					hiddenProgress();
					ImageInfoList info = (ImageInfoList) msg.obj;
					if(info.getGeneral().getStatus() == 1){
						ImageInfo imageinfo = info.getList().get(0);
						float scale = Float.parseFloat(imageinfo.getScale());
						imageView.setCustomScale(scale);
						double rotation = Double.parseDouble(imageinfo.getRotation());
						String[] rotationpoint = imageinfo.getRotationpoint().split(",");
						if(rotationpoint.length == 2){
							PointF pointF = new PointF(Float.parseFloat(rotationpoint[0]), Float.parseFloat(rotationpoint[1]));
							imageView.setCustomRotation(rotation, pointF);
						}else{
							imageView.setCustomRotation(rotation);
						}
						
						float p[] = new float[9];
						String[] array = imageinfo.getPointarray().split(",");
						if(array.length == 9){
							p[0] = Float.parseFloat(array[0]);
							p[1] = Float.parseFloat(array[1]);
							p[2] = Float.parseFloat(array[2]);
							p[3] = Float.parseFloat(array[3]);
							p[4] = Float.parseFloat(array[4]);
							p[5] = Float.parseFloat(array[5]);
							p[6] = Float.parseFloat(array[6]);
							p[7] = Float.parseFloat(array[7]);
							p[8] = Float.parseFloat(array[8]);
							
							imageView.setCustomArrayPoint(p);
						}
					}
					
					Toast.makeText(activity, info.getGeneral().getMessage(), Toast.LENGTH_SHORT).show();
					
					break;

				default:
					break;
				}
			};
		};
		
		getFromNet();
	}

	private void getFromNet() {
		showProgress();
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				ImageInfoList list = LoadData.getImageInfo("1");
				
				Message msg = new Message();
				msg.what = 1;
				msg.obj = list;
				mHandler.sendMessage(msg);
				
			}
		}).start();
		
	}
	
	/**
	 * 显示载入数据的提示信息
	 */
	private void showProgress() {
		if (progress == null) {
			progress = Util.show_process(activity,
					activity.getString(R.string.loading_data), false);
		}
		progress.show();
	}

	/**
	 * 隐藏载入数据的提示信息
	 */
	private void hiddenProgress() {
		if (progress != null) {
			progress.cancel();
			progress = null;
		}
	}
	
}
